<template>
  <div>
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
      <el-form-item prop="name">
        <el-input
          v-model="dataForm.name"
          placeholder="会议室名称"
          size="medium"
          class="input"
          clearable="clearable"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataForm.canDelete"
          class="input"
          placeholder="条件"
          size="medium"
        >
          <el-option label="全部" value="all" />
          <el-option label="可删除" value="true" />
          <el-option label="不可删除" value="false" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="medium" type="primary" @click="searchHandle()"
          >查询</el-button
        >
        <el-button
          size="medium"
          type="primary"
          :disabled="!isAuth(['ROOT', 'MEETING_ROOM:INSERT'])"
          @click="addHandle()"
        >
          新增
        </el-button>
        <el-button
          size="medium"
          type="danger"
          :disabled="!isAuth(['ROOT', 'MEETING_ROOM:DELETE'])"
          @click="deleteHandle()"
        >
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <!--  v-loading 实现加载动画  selection-change  选中触发函数
    :data 是数据源 List 有多少个元素 表格渲染多少行
    
    @selection-change回调函数的参数val传入的是数组格式的数据，内容不是选中记录的id，
    而是选中记录的完整数据。一会儿我们提交Ajax请求的时候，要从中提取出每条选中记录的id值
    -->

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      cell-style="padding: 4px 0"
      style="width: 100%"
      size="medium"
    >
      <!-- 选中项 一列 -->
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      />
      <!-- 序号一列 -->
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="100"
        label="序号"
      >
        <!-- #default="scope"  ==== 作用域插槽的用法, scope="scope" -->
        <!-- 
          1.scope 代表要渲染的每一行数据 
          2. 表格中的每一行的内容 是通过两种形式
              2.1 填充请求发送回来的值 (name) 有多少个name  就渲染多少行
              2.2 没有返回数据 设置每一行的默认内容 如 按钮(修改)
        
        -->
        <template #default="scope">
          <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        min-width="150"
        label="会议室名称"
      />
      <el-table-column
        header-align="center"
        align="center"
        min-width="120"
        label="人数上限"
      >
        <template #default="scope">
          <span>{{ scope.row.max }}人</span>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        min-width="100"
        label="状态"
      >
        <template #default="scope">
          <span>{{ scope.row.status == 1 ? "可使用" : "已停用" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="desc"
        header-align="center"
        align="center"
        label="备注"
        min-width="400"
      />
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template #default="scope">
          <el-button
            type="text"
            size="medium"
            :disabled="
              !isAuth(['ROOT', 'MEETING_ROOM:UPDATE']) || scope.row.id == 0
            "
            @click="updateHandle(scope.row.id)"
          >
            修改
          </el-button>
          <el-button
            type="text"
            size="medium"
            :disabled="
              !isAuth(['ROOT', 'MEETING_ROOM:DELETE']) || scope.row.id == 0
            "
            @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 每次调用更改size 或 count  则重新加载内容 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="loadDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./meeting_room-add-or-update.vue";
export default {
  components: {
    AddOrUpdate,
  },
  created: function () {
    this.loadDataList();
  },

  data: function () {
    return {
      dataForm: {
        name: null,
        canDelete: null,
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      dataRule: {
        name: [
          {
            required: false,
            pattern: "^[a- -Z0-9\u4e00-\u9fa5]{1,20}$",
            message: "会议室名称格式错误",
          },
        ],
      },
    };
  },
  methods: {
    deleteHandle: function (id) {
      let that = this;
      let ids = id
        ? [id]
        : that.dataListSelections.map((item) => {
            return item.id;
          });
      if (ids.length == 0) {
        that.$message({
          message: "没有选中记录",
          type: "warning",
          duration: 1200,
        });
      } else {
        that
          .$confirm(`确定要删除选中的记录？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            that.$http(
              "meeting_room/deleteMeetingRoomByIds",
              "POST",
              { ids: ids },
              true,
              function (resp) {
                if (resp.rows > 0) {
                  that.$message({
                    message: "操作成功",
                    type: "success",
                    duration: 1200,
                  });
                  that.loadDataList();
                } else {
                  that.$message({
                    message: "未能删除记录",
                    type: "warning",
                    duration: 1200,
                  });
                }
              }
            );
          });
      }
    },

    sizeChangeHandle: function (val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.loadDataList();
    },
    currentChangeHandle: function (val) {
      this.pageIndex = val;
      this.loadDataList();
    },
    searchHandle: function () {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$refs["dataForm"].clearValidate();
          if (this.dataForm.name == "") {
            this.dataForm.name = null;
          }
          if (this.pageIndex != 1) {
            this.pageIndex = 1;
          }
          this.loadDataList();
        } else {
          return false;
        }
      });
    },

    loadDataList: function () {
      let that = this;
      that.dataListLoading = true;
      let data = {
        name: that.dataForm.name,
        canDelete:
          that.dataForm.canDelete == "all" ? null : that.dataForm.canDelete,
        page: that.pageIndex,
        length: that.pageSize,
      };
      that.$http(
        "meeting_room/searchMeetingRoomByPage",
        "POST",
        data,
        true,
        function (resp) {
          let page = resp.page;
          that.dataList = page.list;
          that.totalCount = page.totalCount;
          that.dataListLoading = false;
          console.log("返回数据", resp);
        }
      );
    },

    selectionChangeHandle: function (val) {
      this.dataListSelections = val;
    },
    sizeChangeHandle: function (val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.loadDataList();
    },
    currentChangeHandle: function (val) {
      this.pageIndex = val;
      this.loadDataList();
    },
    addHandle: function () {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(); // init 是否传参 决定是新增还是更新传值给其　赋值
      });
    },
    updateHandle: function (id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
  },
};
</script>

<style lang="less"></style>
